package com.xiaotu.spider.qq.pageprocessor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.ProfessionalCommentModel;
import com.xiaotu.common.model.TVPubCommentModel;
import com.xiaotu.common.model.TVRecordModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.TVProcessUtils;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

/**
 * @类名 QQPageProcessor
 * @日期 2017年3月3日
 * @作者 高海军
 * @功能 腾讯视频数据处理类
 */
@Component("QQPageProcessor")
@Scope("prototype")
public class QQPageProcessor extends APageProcessor
{
    @Override
    protected Request getStartRequest(JSONObject target)
    {
        return new Request("http://s.video.qq.com/search?comment=1&stype=0"
                + "&plat=2&otype=json&query="
                + KeywordEncode.encode(target.getString("tvName"))
                + "&pver=0&tabid=0&sort=0&cur=0"
                + "&num=0&start=0&end=20&stag=txt.historyword");
    }

    @Override
    public void process(Page page)
    {
        Map<String, Object> tvMap = page.getRequest().getExtras();
        switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page, tvMap);
                break;
            case Constants.PageDataType.TV_RECORD:
                this.tvProcess(page);
                break;
            case Constants.PageDataType.TV_COMMENT:
                this.commentProcess(page, tvMap);
                break;
            case Constants.PageDataType.TV_PRO_COMMENT:
                this.proCommentProcess(page, tvMap);
                break;
            default:
                break;
        }
    }

    private void tvProcess(Page page)
    {
        TVRecordModel tvRecord = (TVRecordModel) page.getRequest().getExtra("tvRecord");
        tvRecord.setPlayCount(page.getHtml().xpath("//em[@id='mod_cover_playnum']/text()").get());
        page.putField(TVRecordModel.class.getName(), tvRecord);
    }

    private void searchListProcess(Page page, Map<String, Object> tvMap)
    {
        String searchList = page.getRawText();
        if (StringUtils.isEmpty(searchList))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        searchList = searchList.replace("QZOutputJson=", StringUtils.EMPTY);
        searchList = searchList.substring(0, searchList.lastIndexOf(";"));
        JSONObject searchJson = JSONObject.fromObject(searchList);
        if (searchJson == null || searchJson.isEmpty()
                || !searchJson.has("list"))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        JSONArray array = searchJson.getJSONArray("list");
        if (null == array || array.size() == 0)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        boolean matched = false;
        for (int i = 0; i < array.size(); i++)
        {
            JSONObject item = array.getJSONObject(i);
            if (TVProcessUtils.tvMatched(tvMap, item.getString("title"), TVProcessUtils.getTVType
                            (item.getString("BC")), Arrays.asList(item.getString("BM").split(";")),
                    Arrays.asList(item.getString("BD").split(";"))) && "qq".equals(item.getString
                    ("PLNAME")))
            {
            	if(item.getString("imgtag").contains("片花") || item.getString("imgtag").contains("预告")){ // 片花或预告片
            		continue;
            	}
                /*if (item.has("istrailer") && "1".equals(item.getString("istrailer")))// 预告片
                    continue;*/

                String updateSet = (item.has("TT") && item.getString("TT").contains("更新")) ?
                        RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, item.getString("TT"))
                        : null;// 更新集数
                PageUtils.addTargetRequest(page, item.getString("AW"), PageUtils
                        .getNewExtraMapFromPage(page, new String[]{"tvRecord"}, new Object[]{new
                                TVRecordModel(tvMap.get("tvid") + "", item.getString("title") +
                                "", Constants.SiteName.QQTV, (Float.valueOf(item.getString("VC"))
                                / 10) + "", null, null, null, null, null, updateSet,item.getString("AY"))}));
                matched = true;

                PageUtils.addTargetRequest(page,
                        "http://video.coral.qq.com/filmreviewr/c/upcomment/" + item.get("ID") +
                                "?reqnum=10&commentid=&callback=result", tvMap);// 添加专业影评链接

                String commentId = HttpUtils.httpGetJSONP("http://sns.video.qq" +
                        ".com/fcgi-bin/video_comment_id?otype=json&" +
                        "callback=jQuery191026196013600565493_1442389818168&low_login=1" +
                        "&op=3&cid=" + item.getString("ID") + "&_=1442389818169");
                if (StringUtils.isNotEmpty(commentId) && commentId.contains("{") && commentId
                        .contains("}"))
                {
                    commentId = commentId.substring(commentId.indexOf("{"), commentId.lastIndexOf
                            ("}") + 1);
                    JSONObject commentObject = JSONObject.fromObject(commentId);

                    PageUtils.addTargetRequest(page, "http://coral.qq.com/article/" +
                            commentObject.get("comment_id") +
                            "/comment?commentid=0&reqnum=10&tag=&callback" +
                            "=jQuery111106699177431873977_1442473902977&_=1442473902978", tvMap);
                    // 添加评论链接
                }
            }
        }
        if (!matched)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
    }

    private void commentProcess(Page page, Map<String, Object> tvMap)
    {
        String commentListJSONP = page.getRawText();
        if (StringUtils.isEmpty(commentListJSONP))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        commentListJSONP = commentListJSONP.substring(commentListJSONP.indexOf("{"),
                commentListJSONP.lastIndexOf("}") + 1);

        JSONObject commentListObject = JSONObject.fromObject(commentListJSONP);

        if (commentListObject == null || commentListObject.isEmpty() || !commentListObject.has
                ("data"))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        JSONArray commentList = commentListObject.getJSONObject("data").getJSONArray("commentid");

        if (null != commentList && commentList.size() > 0)
        {
            List<TVPubCommentModel> list = new ArrayList<TVPubCommentModel>();
            for (int i = 0; i < commentList.size(); i++)
            {
                JSONObject comment = commentList.getJSONObject(i);
                list.add(new TVPubCommentModel(tvMap.get("tvid") + "", tvMap.get("tvName") + "",
                        Constants.SiteName.QQTV, comment.getJSONObject("userinfo").getString
                        ("nick"), comment.getString("content"), comment.getString("time"), null,
                        page.getUrl().toString()));

            }
            page.putField(TVPubCommentModel.class.getName(), list);
            return;
        }
        throw new SpiderException(SpiderExceptionCode.DataInvalideException);
    }

    private void proCommentProcess(Page page, Map<String, Object> tvMap)
    {
        String profComment = page.getRawText();
        profComment = profComment.substring(7, profComment.length() - 1);
        JSONObject pcmtObject = JSONObject.fromObject(profComment);

        if (pcmtObject == null || pcmtObject.isEmpty() || !pcmtObject.has("data"))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        JSONArray pcommentList = pcmtObject.getJSONObject("data").getJSONArray("commentid");

        if (null != pcommentList && pcommentList.size() > 0)
        {
            List<ProfessionalCommentModel> list = new ArrayList<ProfessionalCommentModel>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            for (int i = 0; i < pcommentList.size(); i++)
            {
                JSONObject comment = pcommentList.getJSONObject(i);
                // 评论内容
                ProfessionalCommentModel commentModel = new ProfessionalCommentModel();

                Date data = new Date(Long.valueOf(comment.getString("time")) * 1000);
                commentModel.setCommentdate(sdf.format(data));
                commentModel.setCommentuser(comment.getJSONObject("userinfo").getString("nick"));
                commentModel.setTitle(comment.getString("title"));
                commentModel.setComment(HtmlUtil.delHTMLTag(comment.getString("content")));
                commentModel.setUrl(page.getUrl().toString());
                commentModel.setUp(comment.getString("up"));
                commentModel.setDown(comment.getString("poke"));
                commentModel.setTvName(tvMap.get("tvName") + "");
                commentModel.setTvId(tvMap.get("tvid") + "");
                commentModel.setResourcesiteid(Constants.SiteName.QQTV);
                list.add(commentModel);
            }
            page.putField(ProfessionalCommentModel.class.getName(), list);
            return;
        }
        throw new SpiderException(SpiderExceptionCode.DataInvalideException);
    }

    @Override
    public String getSpiderType()
    {
        return Constants.SpiderTaskType.TV_QQTV;
    }

    @Override
    public String getPageDataType(Page page)
    {
        String url = page.getUrl().toString();
        if (url.contains("http://s.video.qq.com/search?comment=1&stype=0"))
            return Constants.PageDataType.SEARCH_LIST;
        else if (url.contains("http://coral.qq.com/article/"))
            return Constants.PageDataType.TV_COMMENT;
        else if (url.contains("v.qq.com/x/cover"))
            return Constants.PageDataType.TV_RECORD;
        return Constants.PageDataType.TV_PRO_COMMENT;
    }
}
